package com.weibo;

import java.awt.event.InputEvent;

import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;

public class WeiBoLoginListener extends WebBrowserAdapter{
		
	private String userName;
	private String pwd;
	
	public WeiBoLoginListener( String userName, String pwd ){
		this.userName = userName;
		this.pwd = pwd;
	}
	
	public void loadingProgressChanged( WebBrowserEvent e ) {
			
		if (e.getWebBrowser().getLoadingProgress() == 100 ) {
			
			System.out.println( "开始自动登录" );
			
			String script = "";

			script += " document.getElementById('login_form_savestate').checked=false;"
					+ SinaWeibo.LS;
			script += "var username = document.getElementsByName( 'username' )[0];"
					+ SinaWeibo.LS;
			script += "var password = document.getElementsByName( 'password' )[0];"
					+ SinaWeibo.LS;

			script += " username.click();username.value = '"+this.userName+"';"
					+ SinaWeibo.LS;
			script += " password.value = '"+this.pwd+"';" + SinaWeibo.LS;
			
			//执行js
			e.getWebBrowser().executeJavascript(script);
			
//			//点击密码
//			SinaWeibo.REBOT.mouseMove(975, 462); // 移动
//			SinaWeibo.REBOT.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
//			SinaWeibo.REBOT.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
			
			try {
				Thread.sleep( 500 );
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			
			// 点击登录
			SinaWeibo.REBOT.mouseMove(927, 535); // 移动
			SinaWeibo.REBOT.delay(1000);
			SinaWeibo.REBOT.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
			SinaWeibo.REBOT.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
			
			SinaWeibo.removeListener( this );
		}
	}
	
}
